<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class song_mod extends CI_Model {

    function song_mod()
    {
        parent::__construct();
    }
    
    function get_songs($orderby = "insert_date desc")
    {
        $this->db->select('id, title, artist, album, genre, country, path_thumbnail');
        $this->db->select('(select sum(rate_song.rate)/count(rate_song.id) from rate_song where rate_song.id_song = m_song.id) as rate', FALSE);
        $this->db->order_by($orderby);
        $i = $this->db->get('m_song');

         $var = ($i->num_rows() > 0) ? $i->result_array() : false;
         
         $result = array();
         if($var != false) {
            foreach($var as $row) {
                $rtsp = $this->_getRTSP($row['id']);
                $row['rtsp_video'] = $rtsp['video'];
                $row['rtsp_audio'] = $rtsp['audio'];
                $result[] = $row; 
            }
         }
             
         return $result;
    }
    
    function get_songs_limit($orderby = "insert_date desc", $offset = 0, $limit = 20)
    {
        $this->db->select('id, title, artist, album, genre, country, path_thumbnail');
        $this->db->select('(select sum(rate_song.rate)/count(rate_song.id) from rate_song where rate_song.id_song = m_song.id) as rate', FALSE);
        $this->db->order_by($orderby);
        $this->db->limit($limit,$offset);
        $i = $this->db->get('m_song');

         $var = ($i->num_rows() > 0) ? $i->result_array() : false;
         
         $result = array();
         if($var != false) {
            foreach($var as $row) {
                $rtsp = $this->_getRTSP($row['id']);
                $row['rtsp_video'] = $rtsp['video'];
                $row['rtsp_audio'] = $rtsp['audio'];
                $result[] = $row; 
            }
         }
             
         return $result;
    }
    
    function get_song($id,$type)
    {
        $this->db->select('title, artist, album, path_file');
        $this->db->where('m_song.id', $id);
        $this->db->from('m_song');
        $this->db->join('m_song_file', "m_song_file.id_song = m_song.id AND m_song_file.type = '".$type."'");
        $i = $this->db->get();

        return $var = ($i->num_rows() > 0) ? $i->row_array() : false;
    }
    
    function search_song($keyword = "", $field = "title")
    {
        if($keyword != '') {
            $this->db->select('title, artist, album, genre, path_thumbnail');
            $this->db->like($field, $keyword); 
            $this->db->order_by($field, "asc");
            $i = $this->db->get('m_song');

            return $var = ($i->num_rows() > 0) ? $i->result_array() : false;
        } else {
            return false;
        }
        
    }
    
    function get_playlist($username)
    {
        $this->db->select('playlist_name, title, artist, album, genre');
        $this->db->from('user_playlist_detail');
        $this->db->join('user_playlist', "user_playlist.id = user_playlist_detail.id_user_playlist AND user_playlist.username = '".$username."'");
        $this->db->join('m_song', 'm_song.id = user_playlist_detail.id_song');
        $i = $this->db->get();

        $var = ($i->num_rows() > 0) ? $i->result_array() : false;
        return $var;
    }
    
    function get_records()
    {
        $this->db->select('username, user_record.id, title, album');
        $this->db->from('user_record');
        $this->db->join('m_song', 'm_song.id = user_record.id_song');
        $i = $this->db->get();

        $var = ($i->num_rows() > 0) ? $i->result_array() : false;
        return $var;
    }
    
    function get_record($id, $type)
    {
        $this->db->select('username, title, youtube_link, path_file');
        $this->db->from('user_record');
        $this->db->where('user_record.id', $id);
        $this->db->where('user_record.type', $type);
        $this->db->join('m_song', 'm_song.id = user_record.id_song');
        $i = $this->db->get();

        $var = ($i->num_rows() > 0) ? $i->row_array() : false;
        return $var;
    }
    
    function check_user_rate($username, $idsong)
    {
        $this->db->select('id');
        $this->db->where('username', $username);
        $this->db->where('id_song', $idsong);
        $i = $this->db->get('rate_song');

        $var = ($i->num_rows() > 0) ? $i->row_array() : false;
        return $var;
    }

    function add_rate($data=null)
    {
        if($data != null){
            $this->db->insert('rate_song',$data);
        }
    }

    function update_rate($data,$id=0)
    {
        $this->db->where('id', mysql_real_escape_string($id));
        $this->db->update('rate_song', $data);
    }
    
    function check_user_rate_record($username, $idrec)
    {
        $this->db->select('id');
        $this->db->where('username', $username);
        $this->db->where('id_record', $idrec);
        $i = $this->db->get('rate_record');

        $var = ($i->num_rows() > 0) ? $i->row_array() : false;
        return $var;
    }

    function add_rate_record($data=null)
    {
        if($data != null){
            $this->db->insert('rate_record',$data);
        }
    }

    function update_rate_record($data,$id=0)
    {
        $this->db->where('id', mysql_real_escape_string($id));
        $this->db->update('rate_record', $data);
    }
    
    function add_record($data=null)
    {
        if($data != null){
            $this->db->insert('user_record',$data);
        }
    }
    
    function add_log($action, $idsong, $username)
    {
        $data = array(
            "action" => $action,
            "id_song" => $idsong,
            "username" => $username,
            "action_time" => date('Y-m-d H:i:s')
        );
        $this->db->insert('action_logs',$data);
    }

    function _getRTSP($id){
       
        $server =  $this->db->query("select rtsp_server from config")->row_array();
        $path_audio = UPLOAD_PATH_AUDIO;
        $path_video = UPLOAD_PATH_VIDEO;
        
        $rtsp = array();
        
        $ress = $this->db->query("select path_file from m_song_file where id_song = '$id' AND type = 'video'")->row_array();
        if(empty($ress['path_file'])) $ress['path_file'] = null;
        $ext = substr($ress['path_file'], -3);
        $rtsp['video'] = $server['rtsp_server'].$ext.':'.str_replace($path_video.'/', '', $ress['path_file']);
        
        $ress = $this->db->query("select path_file from m_song_file where id_song = '$id' AND type = 'audio'")->row_array();
        if(empty($ress['path_file'])) $ress['path_file'] = null;
        $rtsp['audio'] = $server['rtsp_server'].'mp3:'.str_replace($path_audio.'/', '', $ress['path_file']);
        
        return $rtsp;
        
    }
    
    
}